from django.shortcuts import render
from django.shortcuts import HttpResponse
import json,time
from db.mydb import MyDBConnection
from db.dao.jobs_companies_r import Jobs_companies_r
from db.utils.dfutils import load_json_to_object
from db.utils.logutils import LogUtils
from db.dao.jobs import Jobs
from db.dao.companies import Companies
def jobs_companies_r_index(request):
    current = 1#如果用户不提供，默认看第一页
    pagesize = 5#如果用户不提供，默认每页5条
    if request.method == 'GET':  #大小写
        myDBConnection = MyDBConnection()
        current = request.GET.get("current")
        pagesize = request.GET.get("pagesize")
        print(f'1111 {request.GET.get("current")}')
        offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
        jobs_companies_rs_json = myDBConnection.fetch_data_by_sql(f"select * from jobs_companies_r limit {offset},{pagesize}")
        msg = f'现在查看的是第{current}页，每页大小{pagesize}'
        print(jobs_companies_rs_json)
        code = 200
        rep_json =  {"code":code,"msg":msg,"jobs_companies_rs":jobs_companies_rs_json}
    if bool(request.GET.get('json'))==True:
        return HttpResponse(json.dumps(rep_json), content_type="application/json")
    else:
        return render(request, "jobs_companies_r/index.html", rep_json)

def jobs_companies_r_add(request):
    # p_id = request.POST.get("p_id")
    # p_name = request.POST.get("p_name")
    data = request.body.decode("utf-8") #获得提交的json格式的字符串参数
    myDBConnection =MyDBConnection()  #连接数据库
    jobs_companies_r_json_obj = json.loads(data)  #j字符串转换为json
    jobs_companies_r = load_json_to_object(jobs_companies_r_json_obj,Jobs_companies_r()) #json对象转换为指定对象Jobs_companies_r
    myDBConnection.insert_entity(jobs_companies_r) #新增一条gameevents，如果不需要存到mysql只使用大数据日志系统，注释本句
    #----------关联查询开始-------------
    session = myDBConnection.get_session()
    job = session.query(Jobs).filter(Jobs.j_id==jobs_companies_r.j_id).first()
    print("+++++++++++++++++++++++++")
    print(jobs_companies_r.cp_id)
    companie = session.query(Companies).filter(Companies.cp_id == jobs_companies_r.cp_id).first()
    session.flush()
    session.close()
    #----------关联查询结束-------------
    jobs_companies_r._job = job
    jobs_companies_r._company = companie
    print("________________________")
    print(companie.cp_name)
    print("___________55555555555_____________")
    print(jobs_companies_r._company)
    #------------日志记录开始 ，使用自定义日志记录器LogUtils.log----------
    # path_name = 'd:log'
    path_name = '/usr/ftp/logs/jobs_companies/'
    current_time = time.localtime(time.time())

    current_time_str = time.strftime('%Y-%m-%d', current_time)  # 参数2为格式化字符串
    print(current_time_str)
    file_name = current_time_str + 'jobs_companies.log'
    LogUtils.log(path_name,file_name,jobs_companies_r.format_str())
    # LogUtils.log(path_name,file_name)

    msg = f"{jobs_companies_r.j_id}比赛事件添加成功"
    code = 200
    rep_json = {"code":code,"msg":msg}
    print("_______________________________________--")
    return HttpResponse(json.dumps(rep_json,ensure_ascii=False), content_type="application/json")


def jobs_companies_r_spider_add(data):
    # p_id = request.POST.get("p_id")
    # p_name = request.POST.get("p_name")

    myDBConnection = MyDBConnection()
    events_json_obj = data
    print(events_json_obj)
    events = load_json_to_object(events_json_obj,Jobs_companies_r())
    print(type(events))
    myDBConnection.insert_entity(events)
    EventID = events.cp_id
    print(EventID)
    msg = f"{EventID}求助事件添加成功"
    code = 200
    rep_json = {"code": code, "msg": msg}
    return rep_json



# 三表关联查询公司岗位详细信息
def jobs_companies_inner(request):
    current = 1#如果用户不提供，默认看第一页
    pagesize = 5#如果用户不提供，默认每页5条
    if request.method == 'GET':  #大小写
        myDBConnection = MyDBConnection()
        current = request.GET.get("current")
        pagesize = request.GET.get("pagesize")
        print(f'1111 {request.GET.get("current")}')
        offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
        jobs_companies_inner_json = myDBConnection.fetch_data_by_sql(f"select c.cp_name,j.j_name,c.address,c.cp_phone from companies c inner join jobs_companies_r jc on c.cp_id = jc.cp_id inner join jobs j on j.j_id = jc.j_id limit {offset},{pagesize}")
        msg = f'现在查看的是第{current}页，每页大小{pagesize}'
        print(jobs_companies_inner_json)
        code = 200
        rep_json =  {"code":code,"msg":msg,"jobs_companies_inner":jobs_companies_inner_json}
    if bool(request.GET.get('json'))==True:
        return HttpResponse(json.dumps(rep_json), content_type="application/json")
    else:
        return render(request, "jobs_companies_r/index.html", rep_json)